from django.test import TestCase
from mixer.backend.django import mixer
from scripts.auto_docker import DockerAutomation

from datetime import datetime

class DockerAutomationTestCase(TestCase):    
    def setUp(self):
        self.docker_automation = DockerAutomation()
        self.image = "group04/neurallog:test"
        self.start_time = datetime(2024, 5, 19, 0, 0, 0).timestamp()
        self.end_time = datetime(2024, 5, 19, 0, 10, 0).timestamp()

    def test_create_run_container(self):
        self.docker_automation.create_run_container(self.image, self.start_time, self.end_time)
        # 执行成功
        self.assertIsNotNone(self.docker_automation.container)
        self.docker_automation.remove_docker()

    def test_run_command(self):
        command = ["cat result/result.json"]
        self.docker_automation.create_run_container(self.image, self.start_time, self.end_time)
        exit_code, output = self.docker_automation.container.exec_run(cmd=command)
        # 断言读取成功
        self.assertIsNotNone(output)
        self.docker_automation.remove_docker()

    def test_remove_docker(self):
        self.docker_automation.create_run_container(self.image, self.start_time, self.end_time)
        self.docker_automation.remove_docker()
        # 成功删除
        self.assertIsNotNone(self.docker_automation.container)